package org.lc.httpcollector.tool

class TimeUse {
    private var start = System.currentTimeMillis()

    fun start() {
        start = System.currentTimeMillis()
    }

    fun stop(): String {
        return formatMilliseconds(stopMs())
    }

    fun stopMs() = System.currentTimeMillis() - start

    fun formatMilliseconds(ms: Long): String {
        if (ms < 0) throw IllegalArgumentException("Milliseconds should be non-negative.")
        return when {
            ms >= 86400000 -> "${ms / 86400000}d${(ms % 86400000) / 3600000}h"
            ms >= 3600000 -> "${ms / 3600000}h${(ms % 3600000) / 60000}m"
            ms >= 60000 -> "${ms / 60000}m${(ms % 60000) / 1000}s"
            ms >= 1000 -> "${ms / 1000}s"
            else -> "$ms ms"
        }
    }
}